package controller

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"house-property/common"
	"house-property/model"
	"net/http"
)

func Checkout(ctx *gin.Context) {
	DB := common.InitDB()
	b, _ := ctx.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	unum := m["num"]
	hnum := m["hnum"]
	department := m["department"]
	//if !ishnumExist(DB,hnum){
	//	ctx.JSON(http.StatusOK,gin.H{
	//		"code":2000,
	//		"msg":"房号输入错误，请重新输入",
	//	})
	//}
	if !isTelephoneExist(DB,unum){
		ctx.JSON(http.StatusOK,gin.H{
			"code":2001,
			"msg":"手机号码输入错误，请重新输入",
		})
		return
	}
	if len(department) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 2002,
			"msg":  "部门不能为空",
		})
		return
	}
	var hou model.House
	var d model.Hou_property
	DB.Model(&d).Where("Hnum = ?",hnum  ).Update("hallocation", "否")//修改符号为是
	DB.Where("Name = ?", unum).Delete(&hou)
	ctx.JSON(200, gin.H{
		"code":200,
	})

}
//func ishnumExist(db *gorm.DB,hnum string)bool{
//	var hou model.House
//	db.Where("HNUM=?",hnum ).First(&hou)
//	if hou.Hnum==""{
//		return true
//	}
//	return false
//}
